﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TienTeTuong
{
    public partial class AjaxAddProductToOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<CateDetailProductDTO> lstProDetail = new List<CateDetailProductDTO>();
            OrderDTO curOrder = (OrderDTO)Session["Order"];
            CategoryDetailDTO cateDetail = new CategoryDetailDTO();
            ProductInOrder productInOrder = new ProductInOrder();
            CategoryDetailBUS cateDetailBUS = new CategoryDetailBUS();
            ProductDetailBUS proBUS = new ProductDetailBUS();
            CategoryBUS cateBUS = new CategoryBUS();
            try
            {
                string cateDetailID = Request.Params["CateDetailID"] ?? "";
                string cateID = Request.Params["CateID"] ?? "";
                if (cateDetailID != "" && cateID != "")
                {
                    string cateName = cateBUS.GetCateNameByID(cateID);
                    cateDetail = cateDetailBUS.GetCategoryDetailByID(cateDetailID);
                    productInOrder.CategoryDetailID = cateDetailID;
                    lstProDetail = proBUS.GetListProductDetail(cateDetailID);
                    if (curOrder == null)
                    {
                        OrderDTO order = new OrderDTO();
                        curOrder = order;
                        productInOrder.NameToDisplay = cateName + "/" + cateDetail.Name;
                        foreach (var item in lstProDetail)
                        {
                            double tmpPrice = 0;
                            Double.TryParse(item.Price, out tmpPrice);
                            productInOrder.Products.Add(item);
                            productInOrder.Price += tmpPrice;
                        }
                        productInOrder.CategoryQuantity = 1;
                        productInOrder.CateID = cateID;
                        curOrder.Items.Add(productInOrder);
                    }
                    else
                    {
                        productInOrder.NameToDisplay = cateName + "/" + cateDetail.Name;
                        foreach (var item in lstProDetail)
                        {
                            double tmpPrice = 0;
                            Double.TryParse(item.Price, out tmpPrice);
                            productInOrder.Products.Add(item);
                            productInOrder.Price += tmpPrice;
                        }
                        if (isInOrder(curOrder, productInOrder))
                        {
                            foreach (var item in curOrder.Items)
                            {
                                if (item.CategoryDetailID == cateDetailID)
                                {
                                    item.CategoryQuantity++;
                                }
                            }
                        }
                        else
                        {
                            productInOrder.CategoryQuantity = 1;
                            productInOrder.CateID = cateID;
                            curOrder.Items.Add(productInOrder);
                        }
                    }
                    Session["Order"] = curOrder;
                    Response.Write(curOrder.Items.Count.ToString());
                }
                else
                {
                    Response.Write("false");
                }
            }
            catch
            {
                Response.Write("false");
            }
        }
        private bool isInOrder(OrderDTO order, ProductInOrder item)
        {
            foreach (var a in order.Items)
            {
                if (a.CategoryDetailID.Equals(item.CategoryDetailID))
                {
                    for (var i = 0; i < a.Products.Count; i++)
                    {
                        if (!a.Products[i].ProId.Equals(item.Products[i].ProId))
                        {
                            return false;
                        }
                    }
                    return true;
                }
            }
            return false;
        }
    }
}